package com.example.demo;

import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.LinkedList;

import com.example.demo.service.UserBehaviorAnalysis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.normalClasses.eye.CookieBehavior;
import com.normalClasses.eye.LoginBehavior;
import com.normalClasses.eye.logReader;
import com.normalClasses.eye.userBehavior;

@Repository
public class Analysis {
    @Autowired
    private UserBehaviorAnalysis analyzer;
    //private String logName;
    //public void setLogName(String logName) {
    //this.logName=logName;
    //}

    // public String getLogName() {
    // return this.logName;
    //}

    /**
     * @param userName 需要检查的账号
     * @param logName  需要检查的主体log
     * @return 返回空字符，意味着没有，返回字符，则代表有
     * @throws FileNotFoundException
     * @throws ParseException
     */
    public String getLoginBehaviors(String userName, String logName) throws FileNotFoundException, ParseException {
        //聚类
        LinkedList<userBehavior> behaviors = analyzer.usernameBehavior4(userName, logName);
//    for(userBehavior item:behaviors){ 
//    	System.out.println(item.getInformation()+"\n");	
//    }
        //暂定数字，可以变化
        LoginBehavior loginBehavior = new LoginBehavior(behaviors, 15);
        return loginBehavior.behaviorAnalysis(300);
    }

    /**
     * @param logName 检查的log主体
     * @return 返回error级别的log字符串组
     * @throws FileNotFoundException
     */
    public String[] getErrorBehaviors(String logName) throws FileNotFoundException {
        logReader reader = new logReader(logName);
        String[] errorLog = reader.getSpecificLog("ERROR");

        return errorLog;
    }

    /**
     * @param logName 检查的log主体
     * @return 返回warn级别的log字符串组
     * @throws FileNotFoundException
     */
    public String[] getWarningBehaviors(String logName) throws FileNotFoundException {
        logReader reader = new logReader(logName);
        String[] warningLog = reader.getSpecificLog("WARN");
        return warningLog;
    }

    /**
     * @param logName 检查的log主体
     * @return 返回debug级别的log字符串组
     * @throws FileNotFoundException
     */
    public String[] getDebugBehaviors(String logName) throws FileNotFoundException {
        logReader reader = new logReader(logName);
        String[] debugLog = reader.getSpecificLog("DEBUG");
        return debugLog;
    }

    /**
     * @param userName 针对的账号用户主体
     * @param logName  需要分析的log
     * @return 返回与cookie相关的刷新行为异常的信息，如果是空字符，则没有
     * @throws FileNotFoundException
     * @throws ParseException
     */
    public String getCookieBehaviors(String userName, String logName) throws FileNotFoundException, ParseException {
        //聚类
        LinkedList<userBehavior> behaviors = analyzer.usernameBehavior4(userName, logName);
        //暂定数字，可以变化
        CookieBehavior cookieBehavior = new CookieBehavior(behaviors, 15);
        return cookieBehavior.behaviorAnalysis(300);
    }
}

